package se.webbzon.boltzmann.game.sequence.builder.assignment;

import se.webbzon.boltzmann.game.sequence.AssignmentSequenceNode;
import se.webbzon.boltzmann.game.sequence.Sequence;
import se.webbzon.boltzmann.game.sequence.SequenceNode;
import se.webbzon.boltzmann.game.sequence.builder.AbstractSequenceRow;
import se.webbzon.boltzmann.game.sequence.builder.SequenceBuilder;
import se.webbzon.boltzmann.game.sequence.builder.SequenceRowDeflation;
import se.webbzon.oschi01.worldarea.WorldArea;

public class AssignmentRow extends AbstractSequenceRow {

	// The name of the sequence row
	public static String name = "assignment";
	
	// The variable name and the expression of this row
	private String variable, expression;
	
	/*============================================================
	Constructors
	============================================================*/
	
	/** Creates a new assignment row associated with the
	 * given variable name and expression. **/
	public AssignmentRow(	SequenceBuilder builder, 
							String variable,
							String expression) {
		super(name, builder);
		this.variable = variable;
		this.expression = expression;
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	/** Returns the name of the variable associated with this
	 * assignment row. **/
	public String getVariable() {
		return variable;
	}

	/** Sets the name of the variable associated with this
	 * assignment row. **/
	public void setVariable(String variable) {
		this.variable = variable;
	}

	/** Returns the expression associated with this
	 * assignment row. **/
	public String getExpression() {
		return expression;
	}

	/** Sets the expression associated with this
	 * assignment row. **/
	public void setExpression(String expression) {
		this.expression = expression;
	}

	@Override public String getText() {
		return variable + " = " + expression;
	}

	@Override public int maxLinks() {
		return 1;
	}

	@Override public int minLinks() {
		return 0;
	}

	@Override public SequenceNode compile(WorldArea world, Sequence sequence) {
		return new AssignmentSequenceNode(sequence,variable,expression);
	}

	@Override public void bind(SequenceNode node, SequenceNode[] nodes) {
		AssignmentSequenceNode assignmentNode = 
				(AssignmentSequenceNode) node;
		if (links() == 1) { 
			Integer branch = getLink(0);
			if (branch != null)
				assignmentNode.setBranch(nodes[getLink(0)]);
		}
	}

	@Override public SequenceRowDeflation getDeflation() {
		return new AssignmentRowDeflation(this);
	}

}
